/*
 *  alogd.h
 *
 *  Copyright (C) 2011  Peter Polacik <polacik.p@gmx.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef  _ALOGD_H_
#define  _ALOGD_H_

#include "uthash.h"

#include <stdio.h>
#include <stdlib.h>

/* Error codes */
#define E_SUCC 0
#define E_INVARG 1

#define E_OPEN 10
#define E_READ 11
#define E_WRITE 12
#define E_LOGOPEN 13

#define E_ALLOC 20

#define E_SOCK 30
#define E_BIND 31
#define E_LISTEN 32


#ifndef LOGDIR_DEFAULT
#define LOGDIR_DEFAULT "/var/log/alogd"
#endif

#ifndef BACKLOG_DEFAULT
#define BACKLOG_DEFAULT 5
#endif

#ifndef SOCKPATH_DEFAULT
#define SOCKPATH_DEFAULT "/tmp/alogd.sock"
#endif

#ifndef DEFAULT_CONFFILE
#define DEFAULT_CONFFILE "alogd.conf"
#endif

/* Message types */
#define T_FERR	1
#define T_ERR	2
#define T_WARN	3
#define T_INFO	4
#define T_DEBUG	5
#define T_DEFAULT T_INFO

#ifndef EOM
#define EOM 0x17
#endif

struct appl {
	char *name; /* key */
	int fd;
	UT_hash_handle hh;
};
typedef struct appl appl_t;

struct conf {
	char *sockpath;
	char *logdir;
	int backlog;
};
typedef struct conf conf_t;

struct mesg {
	char *progname;
	int type;
	char *msg;
};
typedef struct mesg mesg_t;

extern int sockfd;
extern appl_t *applist;
extern char *sockpath;

/* usage.c */
extern void printusage(const char *, FILE *, int, int);

/* config.c */
extern conf_t *parse(char *);

/* local.c */
extern int recv_local(conf_t *);

/* logutil.c */
extern char *get_msg(int);
extern mesg_t *parse_msg(char *, int);
extern char *getmsghdr(int);
extern int program_log(int, char *, char *, int);

/* alogd.c */
extern void errorize(char *, int);
extern int addapp(char *, char *);
extern int getfd(char *);
extern int clearappl(void);
extern int removeapp(char *);
extern char *fixname(char *, char);

#endif   /* _ALOGD_H_ */
